<template>
  <el-drawer v-model="drawerState.drawerVisible" size="40%">
    <template #header>
      <h2>{{drawerState.drawerTitle}}</h2>
    </template>
    <el-tabs class="demo-tabs" v-model="teamData.tabActive">
      <el-tab-pane label="教练组人员" name="first">
        <teamPeopleInfo :list="teamData.coachList" :canEdit="true" />
      </el-tab-pane>
      <el-tab-pane label="参赛队员" name="second">
        <teamPeopleInfo :list="teamData.playerList" :canEdit="true" :entryInsurance="teamData.teamAttachmentRegisterInfo.entryInsurance" />
      </el-tab-pane>
      <el-tab-pane label="球队参赛附件" name="third">
        <ul>
          <li v-for="item in teamAttachmentRegisterInfoArr">
            <a :href="item?.url" target="_blank">{{ item?.name }}</a>
          </li>
        </ul>
      </el-tab-pane>
    </el-tabs>
  </el-drawer>
</template>

<script lang="ts" setup>
import { computed, reactive } from 'vue';
import teamPeopleInfo from './teamPeopleInfo.vue';
import { getRaceTeamRegisterInfo } from '@/api/match';
import { ITableRow } from '../teamReview.vue';
import { TeamMemberRoleEnum } from '@/views/competition/config/enum';

const drawerState = reactive<{
  drawerVisible: boolean
  drawerTitle: string
}>({
  drawerVisible: false,
  drawerTitle: '',
})

const teamData = reactive<{
  tabActive: string
  coachList: any[]
  playerList: any[]
  teamAttachmentRegisterInfo: {
    [key in string]: string
  }
}>({
  tabActive: 'first',
  coachList: [],
  playerList: [],
  teamAttachmentRegisterInfo: {}
})

const teamAttachmentRegisterInfoArr = computed(() => {
  // 获取链接名称
  const getName = (url: string) => {
    const preUrl = url.slice(0, url.indexOf('?'))
    return preUrl.slice(preUrl.lastIndexOf('/') + 1)
  }
  
  return Object.keys(teamData.teamAttachmentRegisterInfo).map(key => {
    const url= teamData.teamAttachmentRegisterInfo[key]
    if(url) {
      return {
        name: getName(url),
        url: url
      }
    }
  }).filter(i => i)
})

const showDrawer = (row: ITableRow) => {
  drawerState.drawerVisible = true
  drawerState.drawerTitle = row.teamName
  // 获取球队成员信息
  getRaceTeamRegisterInfo({
    teamId: row.teamId
  }).then(res => {
    const { teamPlayerRegisterInfoList = [], teamAttachmentRegisterInfo = {}} = res.data
    teamData.teamAttachmentRegisterInfo = teamAttachmentRegisterInfo
    teamData.coachList = teamPlayerRegisterInfoList
      .filter((item: any) => [TeamMemberRoleEnum.HEAD_COACH, TeamMemberRoleEnum.ASSISTANT_COACH].includes(item.identityType))
      .reduce((prev: any, cur: any) => {
        return prev.concat(cur.teamPlayerInfoList || [])
      }, [])
     teamData.playerList = teamPlayerRegisterInfoList
      .find(((item: any) => TeamMemberRoleEnum.PLAYER === item.identityType))?.teamPlayerInfoList || []
  })
}



defineExpose({
  showDrawer
})
</script>

<style lang="scss" scoped>

.demo-tabs > .el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}
.demo-tabs{
  margin: 0 15px;
}
</style>